﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class CreateUser : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.PreRender += new EventHandler(UpdateFlashMessage);
            
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            string username = UserNameTextBox.Text.Trim();
            string password = PasswordTextBox.Text.Trim();
            string fullname = FullNameTextBox.Text.Trim();
            string email = EmailTextBox.Text.Trim();
            string retypePassword = RetypePasswordTextBox.Text.Trim();
            if (retypePassword == password)
            {
                UserEntity userEntity = new UserEntity();
                userEntity.Email = email;
                userEntity.FullName = fullname;
                userEntity.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
                userEntity.UserName = username;

                UserController userController = new UserController();
                int result = userController.CreateUser(userEntity);
                if (result == 1)
                {
                    //MassageLabel.Text = "User name has existing!";
                    Session["FlashMessage"] = "Error: User Name Has Existing!";
                    Session["FlashMessageType"] = "error";
                }
                if (result == 0)
                {
                    //MassageLabel.Text = "Create user successful!";
                    Session["FlashMessage"] = "Success: Create User Successful!";
                    Session["FlashMessageType"] = "success";
                }
            }
            else
            {
                //MassageLabel.Text = "Two times' password are different!";
                Session["FlashMessage"] = "Error: Two Times' Password Are Different!";
                Session["FlashMessageType"] = "error";
            }
        }
       
              
       
    }
}